﻿using System;
using System.Collections.Generic;
using System.Text;
using VersionOne.SDK.ObjectModel;
using VersionOne.SDK.ObjectModel.Filters;

namespace ProjectManagement.VersionOne
{
    public class V1Util
    {
        #region v1 server
        /// <summary>
        /// SQL authentication
        /// </summary>
        /// <param name="v1ServerPath"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static V1Instance GetV1Instance(string v1ServerPath, string userName, string password)
        {
            return new V1Instance(v1ServerPath, userName, password);
        }

        /// <summary>
        /// windows authentication
        /// </summary>
        /// <param name="v1ServerPath"></param>
        /// <returns></returns>
        public static V1Instance GetV1Instance(string v1ServerPath)
        {
            return new V1Instance(v1ServerPath);
        }
        #endregion

        #region v1 project 
        public static List<Project> GetV1Projects(string v1ServerPath, string userName, string password)
        {
            V1Instance v1 = V1Util.GetV1Instance(v1ServerPath, userName, password);
            ProjectFilter filter = new ProjectFilter();
            List<Project> projs = new List<Project>();
            Project proj = v1.Get.ProjectByID("Scope:0");
            if (proj != null)
            {
                projs.Add(proj);
                Project[] childProjs = V1Util.GetChildProjects(proj);
                if (childProjs != null && childProjs.Length > 0)
                {
                    projs.AddRange(childProjs);
                }
            }
            return projs;
        }

        public static Project[] GetChildProjects(Project parentProj)
        {
            List<Project> childProjs = new List<Project>();
            foreach (Project childProj in V1Util.GetChildProjects(parentProj))
            {
                childProjs.Add(childProj);
                Project[] grandChildProjs = V1Util.GetChildProjects(childProj);
                if (grandChildProjs != null && grandChildProjs.Length > 0)
                {
                    childProjs.AddRange(grandChildProjs);
                }
            }
            return childProjs.ToArray();
        }

        public static Project GetV1Project(V1Instance v1, string projName)
        {
            return v1.Get.ProjectByName(projName);
        }
        #endregion

        #region story boards
        public static ICollection<Story> GetProjectStories(Project v1Proj)
        {
            StoryFilter filter = new StoryFilter();
            return v1Proj.GetStories(filter);
        }
        #endregion

        #region tasks
        public static ICollection<Task> GetProjectTasks(Project v1Proj)
        {
            TaskFilter filter = new TaskFilter();
            return v1Proj.GetTasks(filter);
        }
        #endregion

        #region securities
        public static Member GetV1Member(V1Instance v1, string username)
        {
            return v1.Get.MemberByUserName(username);
        }
        #endregion
    }
}
